package com.dkd.spare.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.spare.domain.Spare;
import com.dkd.spare.service.ISpareService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;

/**
 * 备件库存Controller
 * 
 * @author xzj
 * @date 2025-09-03
 */
@RestController
@RequestMapping("/spare/spare")
public class SpareController extends BaseController
{
    @Autowired
    private ISpareService spareService;

    /**
     * 查询备件库存列表
     */
    @PreAuthorize("@ss.hasPermi('spare:spare:list')")
    @GetMapping("/list")
    public TableDataInfo list(Spare spare)
    {
        startPage();
        List<Spare> list = spareService.selectSpareList(spare);
        return getDataTable(list);
    }

    /**
     * 导出备件库存列表
     */
    @PreAuthorize("@ss.hasPermi('spare:spare:export')")
    @Log(title = "备件库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Spare spare)
    {
        List<Spare> list = spareService.selectSpareList(spare);
        ExcelUtil<Spare> util = new ExcelUtil<Spare>(Spare.class);
        util.exportExcel(response, list, "备件库存数据");
    }

    /**
     * 获取备件库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('spare:spare:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(spareService.selectSpareById(id));
    }

    /**
     * 新增备件库存
     */
    @PreAuthorize("@ss.hasPermi('spare:spare:add')")
    @Log(title = "备件库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Spare spare)
    {
        return toAjax(spareService.insertSpare(spare));
    }

    /**
     * 修改备件库存
     */
    @PreAuthorize("@ss.hasPermi('spare:spare:edit')")
    @Log(title = "备件库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Spare spare)
    {
        return toAjax(spareService.updateSpare(spare));
    }

    /**
     * 删除备件库存
     */
    @PreAuthorize("@ss.hasPermi('spare:spare:remove')")
    @Log(title = "备件库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(spareService.deleteSpareByIds(ids));
    }
}
